package com.hbu.ware.hbumallware.controller;

import com.hbu.common.utils.PageUtils;
import com.hbu.common.utils.R;
import com.hbu.ware.hbumallware.entity.PurchaseEntity;
import com.hbu.ware.hbumallware.service.PurchaseService;
import com.hbu.ware.hbumallware.vo.PurchaseDoneVO;
import com.hbu.ware.hbumallware.vo.PurchaseMergeVO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 采购信息
 *
 * @author zhaoxiaohan
 * @email 13315217827@163.com
 * @date 2022-10-28 12:04:52
 */
@RestController
@RequestMapping("ware/purchase")
@AllArgsConstructor
public class PurchaseController {

    private PurchaseService purchaseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/received")
    public R received(@RequestBody List<Long> ids){
        purchaseService.getList(ids);
        return R.ok();
    }

    @RequestMapping("/done")
    public R done(@RequestBody PurchaseDoneVO purchaseDone){
        purchaseService.done(purchaseDone);
        return R.ok();
    }

    /**
     * 合并采购需求
     */
    @RequestMapping(value = "/merge", method = RequestMethod.POST)
    public R merge(@RequestBody PurchaseMergeVO purchaseMergeVO) {
        purchaseService.mergePurchase(purchaseMergeVO);
        return R.ok();
    }

    /**
     * 查询未领取的采购单
     */
    @RequestMapping(value = "/unreceive/list", method = RequestMethod.GET)
    public R unreceiveList(@RequestParam Map<String, Object> params) {
        return R.ok().put("page", purchaseService.getUnreceiveList(params));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseEntity purchase) {
        purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PurchaseEntity purchase) {
        purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
